package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillProcessBiz;
import com.egao.orderB.entity.BillProcess;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-17 09:37:30
 * @version ：1.0
 */
@Repository
public class BillProcessBizImpl extends BaseDao implements BillProcessBiz {

	@Override
	public JsonResult list(PageResult2<T> page, BillProcess billProcess, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillProcess> rm = new BeanPropertyRowMapper<BillProcess>(BillProcess.class);
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
        sql.append(",b.state,b.state_time,b.remark,b.create_time");
		sql.append(",b.create_user,b.update_time,b.update_user");
        sql.append(",sognz.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from chf_bill_process b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sognz on sognz.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sognz.deleted=0 and b.bill_id=:bill_id ");
		map.put("bill_id", billProcess.getBillId());
    	if(Utils.isNotEmpty(billProcess)){
//			if(Utils.isNotEmpty(billProcess.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", billProcess.getName());
//			}
			if(Utils.isNotEmpty(billProcess.getSort())&&Utils.isNotEmpty(billProcess.getOrder())){
				sql.append(" order by "+billProcess.getSort()+" "+billProcess.getOrder());
			} else {
				sql.append(" order by b.create_time desc");
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<BillProcess> list(BillProcess billProcess) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<BillProcess> rm = new BeanPropertyRowMapper<BillProcess>(BillProcess.class);
        StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
        sql.append(",b.state,b.state_time,b.remark,b.create_time");
		sql.append(",b.create_user,b.update_time,b.update_user");
        sql.append(",sognz.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from chf_bill_process b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sognz on sognz.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sognz.deleted=0  and b.bill_id=:bill_id ");
		map.put("bill_id", billProcess.getBillId());
    	if(Utils.isNotEmpty(billProcess)){
//			if(Utils.isNotEmpty(billProcess.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", billProcess.getName());
//			}
			if(Utils.isNotEmpty(billProcess.getSort())&&Utils.isNotEmpty(billProcess.getOrder())){
				sql.append(" order by "+billProcess.getSort()+" "+billProcess.getOrder());
			} else {
				sql.append(" order by b.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public BillProcess getById(BillProcess billProcess) {
		RowMapper<BillProcess> rm = new BeanPropertyRowMapper<BillProcess>(BillProcess.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.bill_id");
        sql.append(",b.state,b.state_time,b.remark,b.create_time");
		sql.append(",b.create_user,b.update_time,b.update_user");
        sql.append(",sognz.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from chf_bill_process b ");
        sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
        sql.append(" inner join sys_organization sognz on sognz.organization_id=b.organization_id ");
        sql.append(" where b.deleted=0 and sognz.deleted=0 and b.id=:id ");
		map.put("id", billProcess.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(BillProcess billProcess, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_bill_process (");
        sql.append("organization_id,bill_id,state,state_time");
		sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:bill_id,:state,now()");
		sql.append(",:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("bill_id", billProcess.getBillId());
        map.put("state", billProcess.getState());
        map.put("remark", billProcess.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean saveWidthCreateTime(BillProcess billProcess, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_bill_process (");
        sql.append("organization_id,bill_id,state,state_time");
		sql.append(",remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:bill_id,:state,now()");
		sql.append(",:remark,:create_user,:create_time)");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("bill_id", billProcess.getBillId());
        map.put("state", billProcess.getState());
        map.put("remark", billProcess.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		map.put("create_time", billProcess.getCreateTime());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(BillProcess billProcess, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_bill_process set ");
        sql.append("state=:state,state_time=now() ");
		sql.append(",remark=:remark,update_user=:update_user,update_time=now() where id=:id ");
        map.put("bill_id", billProcess.getBillId());
        map.put("state", billProcess.getState());
        map.put("remark", billProcess.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", billProcess.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(BillProcess billProcess, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(billProcess.getId().length()>0){
			for(String ID : billProcess.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_bill_process set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
